* This do-file makes "Table 1. Baseline Balance"
* File written by Kazuma Takakura kazuma.arukakat@gmail.com (University of Tokyo)
* Last updated on April 27, 2024

set more off
use "$path_data/fninvest_experience.dta", clear

merge 1:1 subj_id using "$path_data/hh_member.dta"
drop _merge


local pardir "$path_tex"

replace age_old = . if age_missing == 1
replace numhh = hh_male_1984+hh_female_1984 if hh_no_sex_info==.
replace numhh = . if numhh == 0
replace hh_working_age_1984 = . if numhh == .
replace hh_male_1984  = . if numhh == .
replace hh_female_1984  = . if numhh == .
replace hh_child_1984 = . if numhh == .
replace risk00_1std = . if risk00_missing == 1

foreach j in n mean sd {
tabstat cc85 success85 high_edu age_old numhh hh_working_age_1984 hh_male_1984 hh_female_1984 hh_child_1984 risk00_1std hyper00 highdisc00 if houseland == 1, stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr1 = r(StatTotal)
}

foreach j in n mean sd {
tabstat cc85 success85 high_edu age_old numhh hh_working_age_1984 hh_male_1984 hh_female_1984 hh_child_1984 risk00_1std hyper00 highdisc00 if houseland == 0, stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct1 = r(StatTotal)
}

matrix n_dif1 = J(1,12,.)
forvalues i = 1/12 {
	matrix n_dif1[1,`i'] = n_tr1[1,`i'] + n_ct1[1,`i']
}


matrix mean_dif1 = J(1,12,.)
forvalues i = 1/12 {
	matrix mean_dif1[1,`i'] = mean_tr1[1,`i'] - mean_ct1[1,`i']
}



foreach dep in cc85 success85 high_edu age_old numhh hh_working_age_1984 hh_male_1984 hh_female_1984 hh_child_1984 risk00_1std hyper00 highdisc00{
	foreach s in mean se pv obs wpv {
		matrix r1_`dep'_`s' = J(1,2,.)
	}

	reg `dep' houseland, vce(bootstrap, reps(1000) cluster(village))
	
	matrix r1_`dep'_temp = r(table)
	matrix r1_`dep'_r1 = e(r2)
	matrix r1_`dep'_obs = e(N)


	foreach j in 1 2 {
	* beta
	matrix r1_`dep'_mean[1,`j'] = r1_`dep'_temp[1,`j']
	* standard error
	matrix r1_`dep'_se[1,`j'] = r1_`dep'_temp[2,`j']
	* p value
	matrix r1_`dep'_pv[1,`j'] = r1_`dep'_temp[4,`j']
	}
}

foreach dep in cc85 success85 high_edu age_old numhh hh_working_age_1984 hh_male_1984 hh_female_1984 hh_child_1984 risk00_1std hyper00 highdisc00{


forvalues item = 1/1 {
		forvalues scale = 1/1 {
		if r1_`dep'_pv[`item', `scale']<=0.01 {
			local s_`dep'_`item'`scale' %3s "***"
		}
		else if (r1_`dep'_pv[`item', `scale']>0.01) & (r1_`dep'_pv[`item', `scale']<=0.05) {
			local s_`dep'_`item'`scale' %2s "**"
		}
		else if (r1_`dep'_pv[`item', `scale']>0.05) & (r1_`dep'_pv[`item', `scale']<=0.10) {
			local s_`dep'_`item'`scale' %1s "*"
		}
		else {
			local s_`dep'_`item'`scale'  ""
		}
	} // forvalues `scale' loop

}  // forvalues `item' loop


} // foreach `dep' loop


/// trimed age
foreach j in n mean sd {
tabstat age_old if houseland == 1 & age_old>=41 & age_old<=60, stat(`j') save
matrix list r(StatTotal)
matrix `j'_agetr1 = r(StatTotal)
}

foreach j in n mean sd {
tabstat age_old if houseland == 0 & age_old>=41 & age_old<=60, stat(`j') save
matrix list r(StatTotal)
matrix `j'_agect1 = r(StatTotal)
}

matrix n_agedif1 = J(1,9,.)
forvalues i = 1/9 {
	matrix n_agedif1[1,`i'] = n_agetr1[1,`i'] + n_agect1[1,`i']
}


matrix mean_agedif1 = J(1,9,.)
forvalues i = 1/9 {
	matrix mean_agedif1[1,`i'] = mean_agetr1[1,`i'] - mean_agect1[1,`i']
}



foreach s in mean se pv obs wpv {
		matrix r1_age_`s' = J(1,2,.)
}

reg age_old houseland if age_old>=41 & age_old<=60, vce(bootstrap, reps(1000) cluster(village))

matrix r1_age_temp = r(table)
matrix r1_age_r1 = e(r2)
matrix r1_age_obs = e(N)

foreach j in 1 2 {
	* beta
	matrix r1_age_mean[1,`j'] = r1_age_temp[1,`j']
	* standard error
	matrix r1_age_se[1,`j'] = r1_age_temp[2,`j']
	* p value
	matrix r1_age_pv[1,`j'] = r1_age_temp[4,`j']
}


forvalues item = 1/1 {
		forvalues scale = 1/1 {
		if r1_age_pv[`item', `scale']<=0.01 {
			local s_age_`item'`scale' %3s "***"
		}
		else if (r1_age_pv[`item', `scale']>0.01) & (r1_age_pv[`item', `scale']<=0.05) {
			local s_age_`item'`scale' %2s "**"
		}
		else if (r1_age_pv[`item', `scale']>0.05) & (r1_age_pv[`item', `scale']<=0.10) {
			local s_age_`item'`scale' %1s "*"
		}
		else {
			local s_age_`item'`scale'  ""
		}
	} // forvalues `scale' loop

}  // forvalues `item' loop



*** Output the table ***
tempname hh2
file open `hh2' using "`pardir'/balancetest.tex", write replace
file write `hh2' "" _newline
file write `hh2' "% Author: Kazuma Takakura" _newline
file write `hh2' "% Date: `c(current_date)'" _newline
file write `hh2' "% Time: `c(current_time)'" _newline
file write `hh2' "" _newline


file write `hh2' "\begin{table}[h!]\footnotesize" _newline
file write `hh2' "  \centering" _newline
file write `hh2' "  \caption{Baseline Balance}" _newline
file write `hh2' "\label{tab:balance}" _newline
file write `hh2' "\scalebox{0.9}{" _newline
file write `hh2' "\begin{threeparttable}" _newline

file write `hh2' "\begin{tabular}{lccccc}\toprule" _newline
file write `hh2' " Dependent Variable & Displaced &  Not Displaced  & Difference & Obs.   \\\midrule" _newline

file write `hh2' "    Years of education in 2000      & " %04.3f (mean_tr1[1,3]) " &   " %04.3f (mean_ct1[1,3]) " & " %04.3f (r1_high_edu_mean[1,1]) `s_high_edu_11' " & " (n_dif1[1,3]) " \\" _newline
file write `hh2' "    					       & [" %04.3f (sd_tr1[1,3]) "] &   [" %04.3f (sd_ct1[1,3]) "] & (" %04.3f (r1_high_edu_se[1,1])             ") &                     \\" _newline

file write `hh2' "    Age in 2000                      & " %04.3f (mean_tr1[1,4]) " &   " %04.3f (mean_ct1[1,4]) " & " %04.3f (r1_age_old_mean[1,1]) `s_age_old_11' " & " (n_dif1[1,4]) " \\" _newline
file write `hh2' "    						   & [" %04.3f (sd_tr1[1,4]) "] &   [" %04.3f (sd_ct1[1,4]) "] & (" %04.3f (r1_age_old_se[1,1])              ") &                      \\" _newline

file write `hh2' "    Age (41-60) in 2000             & " %04.3f (mean_agetr1[1,1]) " &   " %04.3f (mean_agect1[1,1]) " & " %04.3f (r1_age_mean[1,1]) `s_age_11' " & " (n_agedif1[1,1]) " \\" _newline
file write `hh2' "    						   & [" %04.3f (sd_agetr1[1,1]) "] &   [" %04.3f (sd_agect1[1,1]) "] & (" %04.3f (r1_age_se[1,1])              ") &                      \\" _newline

file write `hh2' "    Household size in 1985        & " %04.3f (mean_tr1[1,5]) " &   " %04.3f (mean_ct1[1,5]) " & " %04.3f (r1_numhh_mean[1,1]) `s_numhh_11' " & " (n_dif1[1,5]) " \\" _newline
file write `hh2' "    			  	           & [" %04.3f (sd_tr1[1,5]) "] &   [" %04.3f (sd_ct1[1,5]) "] & (" %04.3f (r1_numhh_se[1,1])                ") &                        \\" _newline

file write `hh2' "    Number of success in 1985   & " %04.3f (mean_tr1[1,2]) " &   " %04.3f (mean_ct1[1,2]) " & " %04.3f (r1_success85_mean[1,1]) `s_success85_11' " & " (n_dif1[1,2]) " \\" _newline
file write `hh2' "    						   & [" %04.3f (sd_tr1[1,2]) "] &   [" %04.3f (sd_ct1[1,2]) "] & (" %04.3f (r1_success85_se[1,1])             ") &                     \\" _newline

file write `hh2' "    Risk aversion in 2000    & " %04.3f (mean_tr1[1,10]) " &   " %04.3f (mean_ct1[1,10]) " & " %04.3f (r1_risk00_1std_mean[1,1]) `s_risk00_1std_11' " & " (n_dif1[1,10]) " \\" _newline
file write `hh2' "    						   & [" %04.3f (sd_tr1[1,10]) "] &   [" %04.3f (sd_ct1[1,10]) "] & (" %04.3f (r1_risk00_1std_se[1,1])              ") &                      \\" _newline

file write `hh2' "    Hyperbolic discounting in 2000  & " %04.3f (mean_tr1[1,11]) " &   " %04.3f (mean_ct1[1,11]) " & " %04.3f (r1_hyper00_mean[1,1]) `s_hyper00_11' " & " (n_dif1[1,11]) " \\" _newline
file write `hh2' "    						   & [" %04.3f (sd_tr1[1,11]) "] &   [" %04.3f (sd_ct1[1,11]) "] & (" %04.3f (r1_hyper00_se[1,1])              ") &                      \\" _newline

file write `hh2' "    High discounting in 2000   & " %04.3f (mean_tr1[1,12]) " &   " %04.3f (mean_ct1[1,12]) " & " %04.3f (r1_highdisc00_mean[1,1]) `s_highdisc00_11' " & " (n_dif1[1,12]) " \\" _newline
file write `hh2' "    			  	           & [" %04.3f (sd_tr1[1,12]) "] &   [" %04.3f (sd_ct1[1,12]) "] & (" %04.3f (r1_highdisc00_se[1,1])                ") &                        \\" _newline

file write `hh2' " Observation & 51 & 348 & & \\" _newline

file write `hh2' "\midrule" _newline

file write `hh2' "\end{tabular}" _newline

file write `hh2' "\begin{tablenotes}" _newline
file write `hh2' "\item \textsuperscript{a}: Treatment group: those who lost all of the land and/or house due to the dam construction." _newline
file write `hh2' "\item \textsuperscript{b}: Standard errors are clustered by village and the procedure is bootstraped 1000 times." _newline
file write `hh2' "\item \textsuperscript{c}: The asterisks ***, **, and * denote the 1 percent, 5 percent, and 10 percent levels of significance, respectively." _newline
file write `hh2' "\end{tablenotes}" _newline
file write `hh2' "\end{threeparttable}" _newline
file write `hh2' "}" _newline
file write `hh2' "\end{table}" _newline

file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline

file close `hh2'
